﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static void getMensalistaXVeiculos( MensalistaVO mensalistaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT IdMensalista, Obs, placa ");
            query.AppendLine("FROM mensalistaxveiculo ");
            query.AppendLine("WHERE IdMensalista = ?IdMensalista ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdMensalista", mensalistaVO.idMensalista);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                mensalistaVO.mensalistaxVeiculoVOs.Clear();

                while (reader.Read())
                {
                    MensalistaxVeiculoVO mensalistaxVeiculoVO = MensalistaxVeiculoVO.CreateMensalistaxVeiculoVO();
                    mensalistaxVeiculoVO.idMensalista = reader.GetInt32("IdMensalista");
                    mensalistaxVeiculoVO.obs = reader.GetString("Obs");
                    mensalistaxVeiculoVO.placa = reader.GetString("Placa");

                    mensalistaVO.mensalistaxVeiculoVOs.Add(mensalistaxVeiculoVO);
                }
                reader.Close();
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setMensalistaXVeiculos(List<MensalistaxVeiculoVO> mensalistaxVeiculoVOs)
        {
            foreach (MensalistaxVeiculoVO mensalistaxVeiculoVO in mensalistaxVeiculoVOs)
            {
                setMensalistaXVeiculo(mensalistaxVeiculoVO);
            }
        }
        public static void setMensalistaXVeiculo(MensalistaxVeiculoVO mensalistaxVeiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE mensalistaxveiculo ");
            query.AppendLine("SET Obs=?Obs, Placa=?Placa ");
            query.AppendLine("WHERE IdMensalista=?IdMensalista and Placa=?Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdMensalista", mensalistaxVeiculoVO.idMensalista);
            cmd.Parameters.AddWithValue("?Obs", mensalistaxVeiculoVO.obs);
            cmd.Parameters.AddWithValue("?Placa", mensalistaxVeiculoVO.placa);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void newMensalistaXVeiculos(List<MensalistaxVeiculoVO> mensalistaxVeiculoVOs)
        {
            foreach (MensalistaxVeiculoVO mensalistaxVeiculoVO in mensalistaxVeiculoVOs)
            {
                newMensalistaXVeiculo(mensalistaxVeiculoVO);
            }
        }
        public static long newMensalistaXVeiculo(MensalistaxVeiculoVO mensalistaxVeiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO mensalistaxveiculo ");
            query.AppendLine("SET IdMensalista=?IdMensalista, Obs=?Obs, Placa=?Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdMensalista", mensalistaxVeiculoVO.idMensalista);
            cmd.Parameters.AddWithValue("?Obs", mensalistaxVeiculoVO.obs);
            cmd.Parameters.AddWithValue("?Placa", mensalistaxVeiculoVO.placa);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delMensalistaXVeiculos(List<MensalistaxVeiculoVO> mensalistaxVeiculoVOs)
        {
            foreach (MensalistaxVeiculoVO mensalistaxVeiculoVO in mensalistaxVeiculoVOs)
            {
                delMensalistaXVeiculo(mensalistaxVeiculoVO);
            }
        }
        public static void delMensalistaXVeiculo(MensalistaxVeiculoVO mensalistaxVeiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM mensalistaxveiculo ");
            query.AppendLine("WHERE IdMensalista=?IdMensalista and Placa=?Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdMensalista", mensalistaxVeiculoVO.idMensalista);
            cmd.Parameters.AddWithValue("?Placa", mensalistaxVeiculoVO.placa);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }
    }
}
